package org.rency.ues.engine.converter;

import org.rency.common.utils.security.Base64Utils;
import org.rency.common.utils.security.X509CertDomain;
import org.rency.common.utils.security.X509CertUtils;
import org.rency.ues.common.enums.EncryptType;
import org.rency.ues.dal.entity.CertificationDO;
import org.rency.ues.domain.model.CertificationDomain;
import org.rency.ues.domain.request.CreateCertificationRequest;
import org.rency.ues.domain.support.Certification;

import java.security.PrivateKey;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;

/**
 * ${todo}
 *
 * @author: Created by user_rcy@163.com on 2016/07/31.
 * @version: V1.0.0
 * @since JDK 1.8
 */
public class CertificationConverter {

    public static List<Certification> convert2(List<CertificationDO> entityList) throws Exception {
        List<Certification> resultList = new ArrayList<Certification>();
        if(entityList == null){
            return resultList;
        }
        for(CertificationDO entity : entityList){
            resultList.add(CertificationConverter.convert2(entity));
        }
        return resultList;
    }

    public static Certification convert2(CertificationDO entity) throws Exception {
        if(entity == null){
            return null;
        }
        Certification cert = new Certification();
        cert.setCreateTime(entity.getCreateTime());
        cert.setUpdateTime(entity.getUpdateTime());
        cert.setSerialNo(entity.getSerialNo());
        cert.setPassword(entity.getCertPassword());
        cert.setValidate(true);
        X509Certificate x509 = X509CertUtils.getX509CertFromBytes(Base64Utils.decodeBase64(entity.getCert()),entity.getCertPassword());
        PrivateKey privateKey = X509CertUtils.getPrivateKeyByPfxFile(Base64Utils.decodeBase64(entity.getCert()),entity.getCertPassword());
        cert.setCertificate(x509);
        cert.setPrivateKey(privateKey);
        cert.setCertBase(Base64Utils.encodeBase64(x509.getEncoded()));
        return cert;
    }


    public static List<CertificationDomain> convert(List<CertificationDO> entityList){
        List<CertificationDomain> resultList = new ArrayList<CertificationDomain>();
        if(entityList == null){
            return resultList;
        }
        for(CertificationDO entity : entityList){
            resultList.add(CertificationConverter.convert(entity));
        }
        return resultList;
    }

    public static CertificationDomain convert(CertificationDO entity){
        if(entity == null){
            return null;
        }
        CertificationDomain domain = new CertificationDomain();
        domain.setId(entity.getId());
        domain.setCert(entity.getCert());
        domain.setCertType(EncryptType.get(entity.getCertType()));
        domain.setCertPassword(entity.getCertPassword());
        domain.setSerialNo(entity.getSerialNo());
        domain.setOrgiEmail(entity.getOrgiEmail());
        domain.setCreateTime(entity.getCreateTime());
        domain.setUpdateTime(entity.getUpdateTime());
        return domain;
    }

    public static CertificationDO convert(CertificationDomain domain){
        if(domain == null){
            return null;
        }
        CertificationDO entity = new CertificationDO();
        entity.setId(domain.getId());
        entity.setSerialNo(domain.getSerialNo());
        entity.setOrgiEmail(domain.getOrgiEmail());
        entity.setCert(domain.getCert());
        entity.setCertPassword(domain.getCertPassword());
        entity.setCertType(domain.getCertType().getCode());
        entity.setCreateTime(domain.getCreateTime());
        entity.setUpdateTime(domain.getUpdateTime());
        return entity;
    }

    public static X509CertDomain build(CreateCertificationRequest request,Long serialNo) {
        X509CertDomain domain = new X509CertDomain();
        domain.setAlias(request.getAliasName());
        domain.setPassword(request.getPassword());
        domain.setOrganizationEmail(request.getOrgiEmail());
        domain.setOrganizationName(request.getOrgiSimpleName());
        domain.setSerialNumber(serialNo);
        return domain;
    }
}